package com.kaiwukj.android.ufamily.mvp.ui.page.keeper;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerKeeperComponent;
import com.kaiwukj.android.ufamily.di.module.KeeperModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperOrderResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.home.message.ChatActivity;
import com.kaiwukj.android.ufamily.mvp.ui.widget.GridSpaceItemDecoration;
import com.kaiwukj.android.ufamily.mvp.ui.widget.RefreshLayout;
import com.kaiwukj.android.ufamily.mvp.ui.widget.order.CustomRatingBar;
import com.qmuiteam.qmui.widget.QMUIEmptyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.KeeperIndexUrl)
public class KeeperIndexActivity extends BaseMvpActivity<KeeperPresenter> implements KeeperContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.container_right)
    ViewGroup containerRight;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;
    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.tv_keeper_desc)
    TextView tvKeeperDesc;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_gender)
    ImageView ivGender;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.rating_keeper)
    CustomRatingBar ratingKeeper;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.rv_action)
    RecyclerView rvList;

    private KeeperServiceAdapter mServiceAdapter;
    private int keeperId = 0;
    private String hxName;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @OnClick(R.id.btn_chat)
    void intent2Chat() {
        ChatActivity.startKeeperChat(this, hxName);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_keeper_index;
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerKeeperComponent.builder()
                .appComponent(appComponent)
                .keeperModule(new KeeperModule(this))
                .build().inject(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvKeeperDesc.setText("这是您的专属管家");
        containerRight.setVisibility(View.VISIBLE);
        containerRight.setOnClickListener(v -> ARouter.getInstance().build(ARouterUrlKt.KeeperOrderHistoryUrl).navigation());
        tvTitle.setText("我的管家");
        tvRight.setText("历史记录");
        rvList.setLayoutManager(new GridLayoutManager(this, 4));
        rvList.addItemDecoration(new GridSpaceItemDecoration(0, 35));
        mServiceAdapter = new KeeperServiceAdapter();
        rvList.setAdapter(mServiceAdapter);
        mServiceAdapter.setOnItemClickListener((adapter, view, position) -> {
            int serviceId = (int) mServiceAdapter.getItemId(position);
            if (serviceId <= 0) {
                ARouter.getInstance().build(ARouterUrlKt.KeeperReportUrl)
                        .withInt("keeperId", keeperId)
                        .navigation();
            } else {
                ARouter.getInstance().build(ARouterUrlKt.KeeperOrderCreateUrl)
                        .withInt("keeperId", keeperId)
                        .withInt("serviceId", serviceId)
                        .withString("serviceName", mServiceAdapter.getItem(position).getServiceName())
                        .withString("startTime", mServiceAdapter.getItem(position).getServiceTimeStart())
                        .withString("endTime", mServiceAdapter.getItem(position).getServiceTimeStop())
                        .navigation();
            }
        });

        refreshLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getMyKeeperInfo();
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        emptyView.show(true);
        mPresenter.getMyKeeperInfo();
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        emptyView.hide();
        refreshLayout.finishRefresh(false);
        if (taskId == KeeperPresenter.UNBIND) {
            showMessage("请先绑定管家");
            new Handler().postDelayed(
                    () -> ARouter.getInstance().build(ARouterUrlKt.MineKeeperContainerUrl).navigation(),
                    1500);
        }
    }

    @Override
    public void showKeeperInfo(KeeperResult result) {
        emptyView.hide();
        refreshLayout.finishRefresh(true);
        keeperId = result.getHousekeeperId();
        hxName = result.getHxName();
        showUI(result);
    }

    @Override
    public void showKeeperList(List<KeeperResult> results) {

    }

    @Override
    public void showKeeperOderList(List<KeeperOrderResult> results) {

    }

    public void showUI(KeeperResult result) {

        Glide.with(this)
                .load(result.getHeadImg())
                .into(ivAvatar);
        tvName.setText(result.getName());
        tvAge.setText(String.format("%s岁", result.getAge()));
        ivGender.setImageResource(result.isBoy() ? R.mipmap.ic_gender_boy : R.mipmap.ic_gender_girl);
        tvPhone.setText(result.getPhone());

        List<KeeperResult.KeeperService> serviceList = result.getServices();
        if (serviceList == null) {
            serviceList = new ArrayList<>();
        }
        KeeperResult.KeeperService service = new KeeperResult.KeeperService();
        service.setId(-1);
        service.setServiceIcon(String.valueOf(R.mipmap.ic_keeper_report));
        service.setServiceName("投诉意见");
        serviceList.add(service);
        mServiceAdapter.setList(serviceList);
    }
}
